package com.day09;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;

public class PanelFrame extends JFrame {
    private static final int WINDOW_WIDTH = 640;
    private static final int WINDOW_HEIGHT = 640;

    private JPanel p1, p2, p3;
    private JSplitPane splitPane, rightSplitPane;
    private JComboBox<String> cb;
    private JTextField jtf;
    private JButton btn;

    public PanelFrame() {
        initComponent();
        addComponent();
        addListener();
        initWindows();

    }

    private void initComponent() {
        p1 = new JPanel();
        p1.setBackground(Color.YELLOW);
        p2 = new JPanel();
        p2.setBackground(Color.BLUE);
        p3 = new JPanel();
        p3.setBackground(Color.RED);
        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        rightSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    }

    private void addComponent() {
        rightSplitPane.setTopComponent(p1);
        rightSplitPane.setBottomComponent(p2);
        rightSplitPane.setDividerLocation(100);
        rightSplitPane.setDividerSize(2);
        rightSplitPane.setEnabled(false);
        splitPane.setLeftComponent(p3);
        splitPane.setRightComponent(rightSplitPane);
        splitPane.setDividerLocation(100);
        splitPane.setDividerSize(2);
        this.add(splitPane);
    }

    private void addListener() {
        cb.addItemListener(
                new ItemListener() {
                    @Override
                    public void itemStateChanged(ItemEvent e) {
                        ComboBoxFrame.Province prov = (ComboBoxFrame.Province) cb.getSelectedItem();
                        jtf.setText(prov.getName());
                    }
                }
        );
        btn.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        int index = cb.getSelectedIndex();
//                        String prov = (String) cb.getSelectedItem();
                        cb.removeItemAt(index);
                    }
                }
        );

    }

    private void initWindows() {
        this.setTitle("下拉框");
        this.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new PanelFrame();
    }
}
